package org.jeecg.modules.online.desform.excel.converter;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.online.desform.vo.widget.DesformOptions;
import org.jeecg.modules.online.desform.vo.widget.DesformWidget;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConvertUtil {

    /**
     * text转value
     */
    public static final int in = 2;
    /**
     * value转text
     */
    public static final int out = 1;

    /**
     * 用户 表名
     */
    public static final String USER_TABLE_NAME = "SYS_USER";
    /**
     * 用户名称字段名
     */
    public static final String USER_REAL_NAME = "REALNAME";
    /**
     * 用户账号字段名
     */
    public static final String USER_LOGIN_NAME = "USERNAME";
    /**
     * ID字段名
     */
    public static final String ID_NAME = "ID";

    /**
     * 部门 表名
     */
    public static final String DEPART_TABLE_NAME = "SYS_DEPART";
    /**
     * 部门名称字段名
     */
    public static final String DEPART_TEXT_NAME = "DEPART_NAME";
    /**
     * 部门 orgCode
     */
    public static final String DEPART_ORG_CODE_NAME = "orgCode";

    /**
     * 转换
     *
     * @param flag       1value转text 2text转value
     * @param dataList
     * @param widgetList
     */
    public static void doConvertDataList(int flag, List<Map<String, Object>> dataList, List<DesformWidget> widgetList) {
        ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
        Map<String, FieldCommentConverter> converterMap = ConvertFactory.getAllConverterByFieldConfig(widgetList);
        for (Map<String, Object> dataMap : dataList) {
            //map循环中无法新增元素 特加此otherValue作为新增元素存储的容器
            Map<String, Object> otherValue = new HashMap<>();
            for (String key : dataMap.keySet()) {
                Object originValue = dataMap.get(key);
                FieldCommentConverter converter = converterMap.get(key);
                if (originValue == null || converter == null) {
                    continue;
                }
                for (DesformWidget widget : widgetList) {
                    if (widget.getModel().equals(key)) {
                        String value = originValue.toString();
                        // System.out.println("\n--- 字典处理前：" + value);
                        // 处理字典
                        DesformOptions options = widget.getOptions();
                        if (options != null) {
                            // 处理字典
                            String dictCode = options.getDictCode();
                            if (StringUtils.isNotBlank(dictCode)) {
                                List<DictModel> dictList = sysBaseAPI.queryDictItemsByCode(dictCode);
                                String[] valueArray = value.split(",");
                                for (int i = 0; i < valueArray.length; i++) {
                                    String temp = valueArray[i];
                                    for (DictModel dictItem : dictList) {
                                        String val, txt;
                                        if (flag == out) {
                                            val = dictItem.getValue();
                                            txt = dictItem.getText();
                                        } else {
                                            val = dictItem.getText();
                                            txt = dictItem.getValue();
                                        }
                                        if (val.equals(temp)) {
                                            valueArray[i] = txt;
                                        }
                                    }
                                }
                                value = StringUtils.join(valueArray, ",");
                            }
                        }
                        // System.out.println("--- 字典处理后：" + value);

                        Object convertedValue = flag == out ? converter.converterToTxt(value) : converter.converterToVal(value);
                        // System.out.println("--- convertedValue：" + convertedValue);

                        otherValue.put(key, convertedValue);
                        break;
                    }
                }
            }
            for (String key : otherValue.keySet()) {
                dataMap.put(key, otherValue.get(key));
            }
        }
    }

}
